<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sortNo" :min="1"></el-input-number>
			</el-form-item>
			<el-form-item label="规则标题" prop="title">
				<el-input v-model="form.title" placeholder="规则标题" clearable maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="文号规则" prop="rule">
				<el-input v-model="form.rule" placeholder="文号规则" clearable maxlength="40"></el-input>
			</el-form-item>
				<el-collapse>
					<el-collapse-item title="标识含义" name="1">
						[yyyy]:四位当前年份<br/>
						[yy]:两位当前年份<br/>
						[MM]:当前年份<br/>
						[dd]:当前日期<br/>
						[hh]:当前时间-12小时制<br/>
						[HH]:当前时间-24小时制<br/>
						[mm]:当前时间-分钟<br/>
						[ss]:当前时间-秒<br/>
						[A]:当前用户账号<br/>
						[U]:当前用户姓名<br/>
						[D]:当前用户部门<br/>
						[R]:六位随机数<br/>
						[NO]:当前序号<br/>
					</el-collapse-item>
				</el-collapse>
			<el-form-item label="起始序号" prop="startNum" style="margin-top: 10px;">
				<el-input-number v-model="form.startNum" placeholder="起始序号" clearable></el-input-number>
			</el-form-item>
			<el-form-item label="当前序号" prop="currentNum">
				<el-input-number v-model="form.currentNum" placeholder="当前序号" clearable></el-input-number>
			</el-form-item>
			<el-form-item label="编号长度" prop="formatLength">
				<el-input-number v-model="form.formatLength" placeholder="编号长度" clearable></el-input-number>
			</el-form-item>
			<el-form-item label="备注" prop="remark" style="margin-top:10px">
				<el-input v-model="form.remark" maxlength="150" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		components: {
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					recordId:'',
					sortNo: 1,
					title:"",
					rule:"",
					startNum:0,
					currentNum:0,
					formatLength:0,
					model:'1',
					remark: ""
				},
				//验证规则
				rules: {
					title: [
						{required: true, message: '规则标题不能为空'}
					],
					rule: [
						{required: true, message: '文号规则不能为空'}
					],
					startNum: [
						{required: true, message: '起始序号不能为空'}
					],
					currentNum: [
						{required: true, message: '当前序号不能为空'}
					],
					formatLength:[
						{required: true, message: 'NO编号长度不能为空'}
					]
				}
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				if(this.mode=="add")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.bpm.bpmNumber.insertBpmNumber.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('success')
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}else if(this.mode=="edit")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.bpm.bpmNumber.updateBpmNumber.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.visible = false;
								this.$emit('success')
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}
			},
			setData(data){
				this.form.recordId = data.recordId
				this.form.sortNo = data.sortNo
				this.form.title = data.title
				this.form.rule = data.rule
				this.form.startNum = data.startNum
				this.form.currentNum = data.currentNum
				this.form.formatLength = data.formatLength
				this.form.model = data.model
				this.form.remark = data.remark
			},
		}
	}
</script>

<style>
</style>
